﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;

namespace GUI.TimKiem
{
    public partial class frmHoaDonBanBuon : Form
    {
        bool checkClose = false;
        public static Entities.HDBanBuon hdbanbuon;
        Entities.HDBanBuon[] hienthi;

        public frmHoaDonBanBuon()
        {
            InitializeComponent();
            dgvHienThi.DataSource = HienThi();
            hienthi = (Entities.HDBanBuon[])dgvHienThi.DataSource;
            dgvHienThi.Focus();
        }

        public frmHoaDonBanBuon(string makh, bool loaiphieu)
        {
            InitializeComponent();
            if (makh != "<F4 - Tra cứu>" && loaiphieu == false)
            {
                hienthi = HienThiTheoMaKH(makh);
                dgvHienThi.DataSource = hienthi;
                dgvHienThi.Focus();
            }

            if (makh != "<F4 - Tra cứu>" && loaiphieu == true)
            {
                hienthi = HienThiKHTraLaiBanBuonTheoMaKH(makh);
                dgvHienThi.DataSource = hienthi;
                dgvHienThi.Focus();
            }

            fix();
            //else
            //{
            //    dgvHienThi.DataSource = HienThi();
            //    dgvHienThi.Focus();
            //}
        }

        public Entities.HDBanBuon[] HienThi()
        {
            Entities.HDBanBuon[] khachhang = Common.StaticVariable.InterfaceObject.SelectHDBanBuon();

            Entities.HDBanBuon[] temp = new Entities.HDBanBuon[khachhang.Length];
            int sl = 0;
            for (int i = 0; i < khachhang.Length; i++)
            {
                if (khachhang[i].Deleted == false)
                {
                    temp[sl] = khachhang[i];
                    sl++;
                }
            }
            khachhang = new Entities.HDBanBuon[sl];
            for (int i = 0; i < khachhang.Length; i++)
            {
                khachhang[i] = temp[i];
            }
            //lbTongSo.Content = Common.Constants.TongSo + khachhang.Length;
            return khachhang;
        }

        public Entities.HDBanBuon[] HienThiTheoMaKH(string makh)
        {
            Entities.HDBanBuon[] khachhang = Common.StaticVariable.InterfaceObject.SelectHDBanBuonTheoMaKH(makh);
            Entities.HDBanBuon[] temp = new Entities.HDBanBuon[khachhang.Length];
            int sl = 0;
            for (int i = 0; i < khachhang.Length; i++)
            {
                if (khachhang[i].Deleted == false)
                {
                    double ttn = Convert.ToDouble(khachhang[i].ThanhToanNgay);
                    double ttnklp = Convert.ToDouble(khachhang[i].ThanhToanSauKhiLapPhieu);
                    double tt = Convert.ToDouble(khachhang[i].TongTienThanhToan);
                    if ((ttn + ttnklp) < tt)
                    {
                        temp[sl] = khachhang[i];
                        sl++;
                    }
                }
            }
            khachhang = new Entities.HDBanBuon[sl];
            for (int i = 0; i < khachhang.Length; i++)
            {
                khachhang[i] = temp[i];
            }
            //lbTongSo.Content = Common.Constants.TongSo + khachhang.Length;
            return khachhang;
        }

        public Entities.HDBanBuon[] HienThiKHTraLaiBanBuonTheoMaKH(string makh)
        {
            Entities.KhachHangTraLaiBanBuon[] khachhang = Common.StaticVariable.InterfaceObject.SelectHDKHTraLaiBanBuonTheoMaKH(makh);
           
            Entities.KhachHangTraLaiBanBuon[] temp = new Entities.KhachHangTraLaiBanBuon[khachhang.Length];
            int sl = 0;
            for (int i = 0; i < khachhang.Length; i++)
            {
                if (khachhang[i].Deleted == false)
                {
                    double ttn = Convert.ToDouble(khachhang[i].ThanhToanNgay);
                    double ttnklp = Convert.ToDouble(khachhang[i].ThanhToanSauKhiLapPhieu);
                    double tt = Convert.ToDouble(khachhang[i].TienBoiThuong);
                    if ((ttn + ttnklp) < tt)
                    {
                        temp[sl] = khachhang[i];
                        sl++;
                    }
                }
            }
            khachhang = new Entities.KhachHangTraLaiBanBuon[sl];
            for (int i = 0; i < khachhang.Length; i++)
            {
                khachhang[i] = temp[i];
            }
            ArrayList array = new ArrayList();
            foreach (Entities.KhachHangTraLaiBanBuon item in temp)
            {
                Entities.HDBanBuon hd = new Entities.HDBanBuon();
                hd.MaHDBanBuon = item.MaKhachHangTraLaiBanBuon;
                hd.TongTienThanhToan = item.TienBoiThuong;
                hd.ThanhToanNgay = item.ThanhToanNgay;
                hd.ThanhToanSauKhiLapPhieu = item.ThanhToanSauKhiLapPhieu;
                array.Add(hd);
            }
           //lbTongSo.Content = Common.Constants.TongSo + khachhang.Length;
            return (Entities.HDBanBuon[])array.ToArray(typeof(Entities.HDBanBuon));
        }

        void fix()
        {
            for (int i = 0; i < dgvHienThi.ColumnCount; i++)
            {
                dgvHienThi.Columns[i].Visible = false;
            }
            dgvHienThi.Columns["MaHDBanBuon"].HeaderText = "Mã Hóa Đơn Bán Buôn";
            dgvHienThi.Columns["MaHDBanBuon"].Visible = true;
            dgvHienThi.Columns["TongTienThanhToan"].HeaderText = "Tổng Tiền";
            dgvHienThi.Columns["TongTienThanhToan"].Visible = true;
            dgvHienThi.Columns["ThanhToanNgay"].HeaderText = "Thanh Toán Ngay";
            dgvHienThi.Columns["ThanhToanNgay"].Visible = true;
            dgvHienThi.Columns["ThanhToanSauKhiLapPhieu"].HeaderText = "Thanh Toán Sau Lập Phiếu";
            dgvHienThi.Columns["ThanhToanSauKhiLapPhieu"].Visible = true;
            dgvHienThi.Columns["GiaTriThe"].HeaderText = "Giá Trị Thẻ";
            dgvHienThi.Columns["GiaTriThe"].Visible = true;
            if (dgvHienThi.RowCount > 0)
            {
                Common.Utilities.CountDatagridview(dgvHienThi);
                dgvHienThi.Focus();
                dgvHienThi.Select();
            }
            dgvHienThi.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            dgvHienThi.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;


        }


        #region Chọn
        void Chon()
        {
            try
            {
                if (hdbanbuon == null)
                {
                    MessageBox.Show("Bạn chưa chọn khách hàng", "Hệ thống cảnh báo", MessageBoxButtons.OK);
                    return;
                }
                checkClose = true;
                this.Close();
            }
            catch
            {
            }
        }
        #endregion

        #region Thoát
        void Thoat()
        {
            try
            {
                if (MessageBox.Show(Common.MSG.MSG2(), Common.MSG.TieuDe, MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    checkClose = true;
                    this.Close();
                }
            }
            catch
            {
            }
        }
        #endregion

        private void Window_Loaded(object sender, EventArgs e)
        {
            this.Closing += new System.ComponentModel.CancelEventHandler(frmTimKiemHDBanHang_Closing);
            // click chuột trái image
            btnThoat.MouseDown += new MouseEventHandler(MouseDown_Chung);
            btnChon.MouseDown += new MouseEventHandler(MouseDown_Chung);
            // text changed
            txtTimKiem.TextChanged += new EventHandler(txtTimKiem_TextChanged);
            // key up
            dgvHienThi.KeyDown += new KeyEventHandler(KeyDown_Chung);
            dgvHienThi.CellClick += new DataGridViewCellEventHandler(dgvHienThi_CellClick);
            dgvHienThi.CellDoubleClick += new DataGridViewCellEventHandler(dgvHienThi_CellDoubleClick);
        }

        void dgvHienThi_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            i = e.RowIndex; if (i < 0)
                return;
            try
            {
                Chon();
            }
            catch
            {
            }
        }

        void frmTimKiemHDBanHang_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            if (!checkClose)
                if (MessageBox.Show(Common.MSG.MSG2(), Common.MSG.TieuDe, MessageBoxButtons.YesNo) == DialogResult.Yes)
                    e.Cancel = checkClose;
                else
                    e.Cancel = !checkClose;
        }
        int i = -1;
        void dgvHienThi_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            for (int i1 = 0; i1 < hienthi.Length; i1++)
            {
                if (hienthi[i1].MaHDBanBuon == dgvHienThi.CurrentRow.Cells["MaHDBanBuon"].Value.ToString())
                {
                    hdbanbuon = hienthi[i1];
                }
            }
        }

        void txtTimKiem_TextChanged(object sender, EventArgs e)
        {
            int soluong = 0;
            if (rdbTimKiem1.Checked == true)
            {
                for (int i = -1; i < hienthi.Length; i++)
                {
                    int kiemtra = hienthi[i].MaKhachHang.ToString().ToUpper().IndexOf(txtTimKiem.Text.ToUpper());
                    if (kiemtra >= 0)
                    {
                        soluong++;
                    }
                }
                if (soluong == 0)
                {
                     dgvHienThi.DataSource = new Entities.HDBanBuon[0];
                    //lbTongSo.Content = Common.Constants.TongSo + "0";
                    return;
                }
                Entities.HDBanBuon[] hienthitheoid = new Entities.HDBanBuon[soluong];
                soluong = 0;
                for (int i = -1; i < hienthi.Length; i++)
                {
                    int kiemtra = hienthi[i].MaKhachHang.ToString().ToUpper().IndexOf(txtTimKiem.Text.ToUpper());
                    if (kiemtra >= 0)
                    {
                        hienthitheoid[soluong] = hienthi[i];
                        soluong++;
                    }
                }
                dgvHienThi.DataSource = hienthitheoid;
                //lbTongSo.Content = Common.Constants.TongSo + hienthitheoid.Length;

            }
        }

        private void MouseDown_Chung(object sender, MouseEventArgs e)
        {
            try
            {
                Button temp = (Button)sender;

                #region chuột trái
                
                    switch (temp.Name)
                    {
                        case "btnChon":
                            {
                                Chon();
                                break;
                            }
                        case "btnThoat":
                            {
                                Thoat();
                                break;
                            }
                        default:
                            break;
                    }
                
                #endregion
            }
            catch
            {
            }
        }

        private void KeyDown_Chung(object sender, KeyEventArgs e)
        {
            try
            {
                if (e.KeyCode == Keys.F2)
                {
                    Chon();
                }
                if (e.KeyCode == Keys.Escape)
                {
                    Thoat();
                }
                if (e.KeyCode == Keys.Enter)
                {
                    Chon();
                }
            }
            catch
            {
            }
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
